using System;

namespace Framework.Core.Cache
{
    internal class CacheEntry
    {
        private object value;

        public CacheEntry(TimeSpan callDuration, object value)
        {
            creationDate = DateTime.Now;
            this.callDuration = callDuration;
            Value = value;
            Hit = 1;
        }

        private readonly DateTime creationDate;
        private TimeSpan callDuration;
        public int Hit { get; private set; }

        public double Rank
        {
            get { return (Hit == 0) ? callDuration.TotalMilliseconds : Hit * callDuration.TotalMilliseconds; }
        }

        public object Value
        {
            get
            {
                Hit++;
                return value;
            }
            private set { this.value = value; }
        }

        public bool IsValid(TimeSpan expiration)
        {
            var now = DateTime.Now;
            var isValid = creationDate + expiration > now;
            return isValid && Hit != 0;
        }

        public void ResetStatistics()
        {
            Hit = 0;
        }
    }
}